﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using SlaughterItem.Common.Domains.Detection;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace SlaughterItem.API.Application.Commands.Week02._PRQS.DelDisablePRQS
{
    /// <summary>
    /// 删除或禁用检疫项目表
    /// </summary>
    public class DelDrisablePRQSHandler : IRequestHandler<DelDisablePRQSCommand, Result>
    {
        public IBaseRepository<PRQSData> PRQSDataRepository { get; set; }
        public DelDrisablePRQSHandler(IBaseRepository<PRQSData> pRQSDataRepository)
        {
            PRQSDataRepository = pRQSDataRepository;
        }
        public async Task<Result> Handle(DelDisablePRQSCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Update };
            var data = await PRQSDataRepository.GetAll().FirstOrDefaultAsync(x => x.PRQSId == request.PRQSId);
            if (data != null)
            {
                if (request.pid == 9)
                {
                    bool UpState = data.IsPRQS == true ? false : true;
                    data.IsPRQS = UpState;
                    data.PRQSstate = UpState;
                    PRQSDataRepository.Update(data);
                    result.Code = (int)CommonErrorCode.Success;
                    return result;
                }
                else if (request.pid == 11)
                {
                    bool UpState = data.ISDeletePRQS == true ? false : true;
                    data.ISDeletePRQS = UpState;
                    PRQSDataRepository.Update(data);
                    result.Code = (int)CommonErrorCode.Success;
                    return result;
                }
                else
                {
                    return result;
                }
            }
            else
            {
                return result;
            }
        }
    }
}
